package com.cqjtu.education.controller;

import com.cqjtu.education.entity.Task;
import com.cqjtu.education.service.TaskService;
import com.cqjtu.education.utils.Result;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Task)表控制层
 *
 * @author makejava
 * @since 2022-04-20 15:50:56
 */
@RestController
@Api(tags = "任务相关接口")
@RequestMapping("task")
public class TaskController {
    /**
     * 服务对象
     */
    @Resource
    private TaskService taskService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/queryById")
    @ApiOperation(value = "queryById", notes = "通过id查询task")
    public Result queryById(@RequestParam("id") String id) {
        try {
            return Result.success(taskService.queryById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    @RequestMapping("/queryAllByCourseId")
    @ApiOperation(value = "queryAllByCourseId", notes = "通过课程id查询所有任务")
    public Result queryAllByCourseId(@RequestParam("courseId") String courseId) {
        try {
            return Result.success(taskService.queryAllByCourseId(courseId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    @RequestMapping("/queryExpiredByCourseId")
    @ApiOperation(value = "queryExpiredByCourseId", notes = "通过课程id查询已过期任务")
    public Result queryExpiredByCourseId(@RequestParam("courseId") String courseId) {
        try {
            return Result.success(taskService.queryExpiredByCourseId(courseId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    @RequestMapping("/queryNotExpiredByCourseId")
    @ApiOperation(value = "queryNotExpiredByCourseId", notes = "通过课程id查询未过期任务")
    public Result queryNotExpiredByCourseId(@RequestParam("courseId") String courseId) {
        try {
            return Result.success(taskService.queryNotExpiredByCourseId(courseId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 新增数据
     *
     * @param task 实体
     * @return 新增结果
     */
    @RequestMapping("/add")
    @ApiOperation(value = "add", notes = "新增任务")
    public Result add(@RequestBody Task task) {
        try {
            return Result.success(taskService.insert(task));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("新增失败");
    }

    /**
     * 编辑数据
     *
     * @param task 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    @ApiOperation(value = "edit", notes = "修改任务")
    public Result edit(@RequestBody Task task) {
        try {
            return Result.success(taskService.update(task));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/deleteById")
    @ApiOperation(value = "deleteById", notes = "删除任务")
    public Result deleteById(@RequestParam("id") String id) {
        try {
            return Result.success(taskService.deleteById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败");
    }

}

